{% extends "shuup/admin/base.jinja" %}
{% from "shuup/admin/macros/general.jinja" import content_block, content_with_sidebar %}

{% block title %}{{ coupon.code or _("New Discount Code") }}{% endblock %}

{% block body_class %}shuup-details{% endblock %}
{% block content %}
    {% call content_with_sidebar(content_id="coupon_form") %}
        <form method="post" id="coupon_form">
            {% csrf_token %}
            {% call content_block(_("General information"), "fa-clock-o") %}
                {% set addon_after = "<a class='btn btn-primary' href='#' id='id_autogenerate'>%s</a>" % _("Generate") %}
                {{ bs3.field(form.code, addon_after=addon_after) }}
                {{ bs3.field(form.usage_limit_customer) }}
                {% set usage_limit_addon = "<small>%s</small>" % _("%(coupons)s used", coupons=coupon.usages.count()) %}
                {{ bs3.field(form.usage_limit, addon_after=usage_limit_addon) }}
                {{ bs3.field(form.shop) }}
                {{ bs3.field(form.supplier) }}
                {{ bs3.field(form.active) }}
            {% endcall %}
        </form>
    {% endcall %}
{% endblock %}

{% block extra_js %}
    <script>
    function generateDiscountCode() {
        var $field = $("#id_code");
        if($field.val().length) {
            alert(gettext("Error! Please empty the code value before using autogenerate."))
        }
        else {
            var code = "";
            var possible = "ACDEFGHKMNPQRSTVWX4679";
            for (var i=0; i < 8; i++)
                code += possible.charAt(Math.floor(Math.random() * possible.length));

            $field.val(code);
        }
    }
    $(document).ready(function(){
        $(document).on('click', '#id_autogenerate', function(e){
            e.preventDefault();
            generateDiscountCode();
        });
    });
    </script>
{% endblock %}
